<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body>
<div id="loginFormDiv">
    <form class="layui-form" action="" lay-filter="example">
        <h3 style="text-align: center; padding-bottom: 30px"> 请登录 </h3>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入邮箱"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input type="text" name="captChar" lay-verify="title" autocomplete="off" placeholder="请输入验证码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <img src="/captcha" style="width:85px;height:38px" id="cap"/>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginFormBtn">立即登录</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

</div>
<script src="./layui/layui.js"></script>
<script>
    layui.use(
        function(){
            var layer = layui.layer
                ,form = layui.form
                ,$=layui.$;

            // layer.msg('Hello World');
            //监听提交
            form.on('submit(loginFormBtn)', function(data){


                //发送ajax请求
                $.ajax({
                    url:"/user/login",
                    type:"post",
                    data:data.field,
                    success:function (res) {
                        if (res.statusCode == "1001"){
                            window.location.href = "index.html"
                        }else {
                            layer.alert(res.message, {
                                title: '提示'
                            })
                        }
                    }
                })


                return false; //取消表单的默认提交行为
            });

            //点击刷新验证码
            $(function(){
                $("#cap").click(function(){
                    //刷新验证码
                    path = $(this).attr("src")+"?"+new Date().getTime();
                    $(this).attr("src",path);
                });
            });
    });
</script>

<style>
    #loginFormDiv {
        margin: 100px auto;
        padding: 30px 30px 20px 50px;
        border: solid 1px blue;
        width: 600px;
        height: 300px;
        box-shadow: 5px 9px 18px 5px #eeeeee;
    }

</style>
</body>
</html>